package cn.fornever.tools.db.assist.util;

import cn.fornever.tools.db.assist.entity.ColumnInfo;
import cn.fornever.tools.db.assist.entity.ForeignKeyInfo;
import cn.fornever.tools.db.assist.entity.PrimaryKeyInfo;
import cn.fornever.tools.db.assist.entity.TableInfo;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBServiceMysqlImpl implements IDBService {

    private static class SingletonHolder{
        public static DBServiceMysqlImpl instance = new DBServiceMysqlImpl();
    }
    private DBServiceMysqlImpl(){
    }
    public static DBServiceMysqlImpl getInstance(){
        return SingletonHolder.instance;
    }

    @Override
    public List<TableInfo> getTableInfo(Connection connection, String catalog, String schemaPattern, String tableNamePattern, String[] types) {
        return DBServiceCommonImpl.getInstance().getTableInfo(connection,catalog,schemaPattern,tableNamePattern,types);
    }


    /**
     * 获取所有列的信息.
     * <p>注意,不同数据库驱动的实现略有不同,不一定支持通配符,也不一定支持null.也不一定是每一个参数都会起作用.
     *
     * @param connection        数据库连接.
     * @param catalog           数据库的catalog
     * @param schemaPattern     数据库的schema名称.
     * @param tableNamePattern  表名.不能为空字符串,必须为有确切含义的表达式,比如'%'
     * @param columnNamePattern 列名.
     * @return 符合要求的所有表的信息.如果出现异常, 则返回 {@code null}.
     */
    @Override
    public List<ColumnInfo> getColoumInfo(Connection connection, String catalog, String schemaPattern,
                                          String tableNamePattern, String columnNamePattern) {
        List<ColumnInfo> result = null;
        DatabaseMetaData metaData = DBServiceCommonImpl.getInstance().getDatabaseMetaData(connection);
        ResultSet columnResultSet;
        try {
            columnResultSet = metaData.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
            result = new ArrayList<>();
            while (columnResultSet.next()) {
                ColumnInfo columnInfo = new ColumnInfo();
                result.add(columnInfo);
                columnInfo.setTableCat(columnResultSet.getString(1));//TABLE_CAT
                columnInfo.setTableSchem(columnResultSet.getString(2));//TABLE_SCHEM
                columnInfo.setTableName(columnResultSet.getString(3));//TABLE_NAME
                columnInfo.setColumnName(columnResultSet.getString(4));//COLUMN_NAME
                columnInfo.setDataType(string2Integer(columnResultSet.getString(5), null));//DATA_TYPE
                columnInfo.setTypeName(columnResultSet.getString(6));//TYPE_NAME
                columnInfo.setColumnSize(string2Integer(columnResultSet.getString(7), null));//COLUMN_SIZE
                columnInfo.setBufferLength(string2Integer(columnResultSet.getString(8), null));//BUFFER_LENGTH
                columnInfo.setDecimalDigits(string2Integer(columnResultSet.getString(9), null));//DECIMAL_DIGITS
                columnInfo.setNumPrecRadix(string2Integer(columnResultSet.getString(10), null));//NUM_PREC_RADIX
                columnInfo.setNullable(string2Integer(columnResultSet.getString(11), null));//NULLABLE
                columnInfo.setRemarks(columnResultSet.getString(12));//REMARKS
                columnInfo.setColumnDef(columnResultSet.getString(13));//COLUMN_DEF
                columnInfo.setSqlDataType(string2Integer(columnResultSet.getString(14), null));//SQL_DATA_TYPE
                columnInfo.setSqlDatetimeSub(string2Integer(columnResultSet.getString(15), null));//SQL_DATETIME_SUB
                columnInfo.setCharOctetLength(string2Integer(columnResultSet.getString(16), null));
                columnInfo.setOrdinalPosition(string2Integer(columnResultSet.getString(17), null));
                columnInfo.setIsNullable(columnResultSet.getString(18));
                columnInfo.setScopeCatlog(columnResultSet.getString(19));
                columnInfo.setScopeSchema(columnResultSet.getString(20));
                columnInfo.setScopeTable(columnResultSet.getString(21));
                columnInfo.setSourceDataType(string2Short(columnResultSet.getString(22), null));
                columnInfo.setIsAutoincrement(columnResultSet.getString(23));
                columnInfo.setIsGeneratedcolumn(columnResultSet.getString(24));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        result = setJavaType(result,connection,catalog, schemaPattern, tableNamePattern);
        return result;
    }

    @Override
    public List<PrimaryKeyInfo> getPrimaryKeyInfo(Connection connection, String catalog, String schemaPattern, String tableNamePattern) {
        return null;
    }

    @Override
    public List<ForeignKeyInfo> getForeignKeyInfo(Connection connection, String catalog, String schema, String tableName) {
        return null;
    }
}
